package cn.telling.sellercentre.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.lob.OracleLobHandler;
import org.springframework.stereotype.Repository;

import cn.telling.product.dao.Impl.ProductSortDaoImpl;
import cn.telling.sellerCenter.vo.ProductMainVO;
import cn.telling.sellercentre.dao.IProductMainDao;
import cn.telling.utils.AutoInjection;
import cn.telling.utils.BaseUtil;

/**
 * @Title: ProductMainDao.java
 * @Package com.product.dao.Impl
 * @Description: TODO(描述该文件做什么)
 * @author luoqingm
 */

@Repository("productMainDao")
public class ProductMainDaoImpl implements IProductMainDao {
	
	private static Logger logger = Logger.getLogger(ProductSortDaoImpl.class);
	@Autowired
	@Qualifier("film-template")
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	// 大字段处理器
	@Autowired
	@Qualifier("oracleLobHandler")
	private OracleLobHandler oracleLobHandler;

	public OracleLobHandler getOracleLobHandler() {
		return oracleLobHandler;
	}

	public void setOracleLobHandler(OracleLobHandler oracleLobHandler) {
		this.oracleLobHandler = oracleLobHandler;
	}
	
	/**
	 * 
	 * @Description:根据产品产品id查询商品
	 * @param 参数说明
	 * @return 返回值
	 * @author luoqingm
	 */
	@Override
	public ProductMainVO getProductMainByProductId(String productId) {
		// TODO Auto-generated method stub
		String sql = " SELECT p.productId, "
				+ "   p.productName,"
				+ "   p.productPattern,"
				+ "   p.productCode,"
				+ "   p.sortId,"
				+ "   p.brandId,"
				+ "   p.color,"
				+ "   p.barCode,"
				+ "   p.isHaveImei,"
				+ "   p.priceRetailGuide,"
				+ "   p.specificationParameter,"
				+ "   p.description,"
				+ "   p.remark,"
				+ "   p.createuserId,"
				+ "   p.modifyuserId,"
				+ "   p.createTime,"
				+ "   p.modifyTime,"
				+ "   to_char(p.marketTime,'yyyy-mm-dd') as marketTime,"
				+ "   p.del_flag, "
				+ "   p.productBelongs ,"
				+ "   p.evaluation ,"
				+ "   p.service, "
				+ "   p.publish, "
				+ "   p.packinglist, "
                + "   p.SAP_PRODUCT_CODE as sapProductCode, "
                + "   p.IS_IN_SAP_SALES as isInSapSales "
				+ "from product_main p"
				+ "  left join product_sort ps on ps.sortid=p.sortid"
				+ "  where p.del_flag='1' and p.productId= ?";

		Object[] params = { productId };
		logger.debug("查询单个产品:" + BaseUtil.logSQL(sql, params));
		
		List<ProductMainVO> productList = getJdbcTemplate().query(sql, params,
                new RowMapper<ProductMainVO>() {
            public ProductMainVO mapRow(ResultSet rs, int rowNum)
                    throws SQLException {
                ProductMainVO vo = new ProductMainVO();
                AutoInjection.Rs2Vo(rs, vo, oracleLobHandler);
                return vo;
            }
        });
		if(productList != null && productList.size() > 0) {
		    return productList.get(0);
		}
		return null;
	}

    @Override
    public ProductMainVO getProductMainByShopProductId(String shopProductId) {
        String sql = " SELECT p.productId, "
                + "   p.productName,"
                + "   p.productPattern,"
                + "   p.productCode,"
                + "   p.sortId,"
                + "   p.brandId,"
                + "   p.color,"
                + "   p.barCode,"
                + "   p.isHaveImei,"
                + "   p.priceRetailGuide,"
                + "   p.specificationParameter,"
                + "   p.description,"
                + "   p.remark,"
                + "   p.createuserId,"
                + "   p.modifyuserId,"
                + "   p.createTime,"
                + "   p.modifyTime,"
                + "   to_char(p.marketTime,'yyyy-mm-dd') as marketTime,"
                + "   p.del_flag, "
                + "   p.productBelongs ,"
                + "   p.evaluation ,"
                + "   p.service, "
                + "   p.publish, "
                + "   p.packinglist, "
                + "   p.SAP_PRODUCT_CODE as sapProductCode, "
                + "   p.IS_IN_SAP_SALES as isInSapSales "
                + "from product_main p"
                + "  left join shop_product sp on sp.PRODUCTID=p.productId"
                + "  where p.del_flag='1' and sp.id= ?";

        Object[] params = { shopProductId };
        logger.debug("根据产品指定供应商ID查询单个产品:" + BaseUtil.logSQL(sql, params));
        
        List<ProductMainVO> productList = getJdbcTemplate().query(sql, params,
                new RowMapper<ProductMainVO>() {
            public ProductMainVO mapRow(ResultSet rs, int rowNum)
                    throws SQLException {
                ProductMainVO vo = new ProductMainVO();
                AutoInjection.Rs2Vo(rs, vo, oracleLobHandler);
                return vo;
            }
        });
        if(productList != null && productList.size() > 0) {
            return productList.get(0);
        }
        return null;
    }


}
